package com.itheima.common;

import com.itheima.domian.Employee;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//全局异常处理器
@RestControllerAdvice
public class GlobalExceptionHandler {

    //捕获重名异常
  @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> handlerSqlException(SQLIntegrityConstraintViolationException e){
      String message = e.getMessage();
      if (message.contains("Duplicate entry")) {
          String username = message.split(" ")[2];
          return R.error(username+"用户名已存在");
      }
        return R.error("服务器正在维护");
  }

  //捕获自定义异常
    @ExceptionHandler(CustomerException.class)
    public R<String> handlerSqlException(CustomerException e){
        return R.error(e.getMessage());
    }

}
